@extends('backstage.layout.master')
@section('title', '库存导出')
@section('style')
    <link rel="stylesheet" href="backstage/assets/css/jquery-ui-1.10.3.custom.min.css"/>
    <link rel="stylesheet" href="backstage/assets/css/chosen.css"/>
    <link rel="stylesheet" href="backstage/assets/css/datepicker.css"/>
    <link rel="stylesheet" href="backstage/assets/css/bootstrap-timepicker.css"/>
    <link rel="stylesheet" href="backstage/assets/css/daterangepicker.css"/>
    <link rel="stylesheet" href="backstage/assets/css/colorpicker.css"/>
    <style>
        .ace-file-input input[type=file] {
            position: static;
        }

        .ace-nav {
            height: 10px;
        }

        .icon-animated-bell {
            margin-top: 13px;
        }
        .sel-div{
            /*text-align:center;*/
            width: 50%;
            margin-left: 25%;
        }
    </style>
@stop
@section('content')
    <div class="breadcrumbs" id="breadcrumbs">
        <script type="text/javascript">
            try {
                ace.settings.check('breadcrumbs', 'fixed')
            } catch (e) {
            }
        </script>

        <ul class="breadcrumb">
            <li>
                <i class="icon-home home-icon"></i>
                <a target="_blank" href="/backstage/home">主页</a>
            </li>

            <li class="active">
                <a target="_blank" href="/backstage/product-temp/product-temp-list">产品模板</a>
            </li>
        </ul><!-- .breadcrumb -->
    </div>
    <div class="page-content">
        <div class="page-header">
            <h1>
                产品模板
                <small>
                    <i class="icon-double-angle-right"></i>
                    库存导出
                </small>
                <span class="pull-right">
                    <button class="btn btn-primary" onclick="javascript:window.location.href='{{asset('')}}backstage/product-temp/stocks-import';">库存信息导入</button>
                </span>
            </h1>
        </div>
        <div class="row">
            <div class="col-sm-12 sel-div">
                <div class="well">
                    <form action="javascript:void(0)" name="searchForm">
                        <img src="images/icon_search.gif" width="26" height="22" border="0" alt="SEARCH">
                        <!-- 品牌 -->
                        导出条件:<select name="brand_id" id="brand_id">
                            <option value=0>全部品牌</option>
                            @foreach($brands as $brand)
                                <option @if($brand_id===$brand->brand_id) selected @endif value={{isset($brand->brand_id)?$brand->brand_id:0}}>
                                    {{isset($brand->brand_name)?$brand->brand_name:''}}</option>
                            @endforeach
                        </select>
                        类型：<select name="category_id" id="category_id">
                            @foreach($temp_categorys as $temp_category)
                                <option @if($temp_category_id===$temp_category->id) selected @endif value={{isset($temp_category->id)?$temp_category->id:0}}>{{isset($temp_category->name)?$temp_category->name:''}}</option>
                            @endforeach
                        </select>
                        <!-- 关键字 -->
                        <input type="button" id="filter_btn" value=" 搜索 " class="button">
                    </form>
                </div>
            </div>
            <div class="col-xs-12">
                <!-- PAGE CONTENT BEGINS -->
                <form class="form-horizontal" role="form" action="/backstage/product-temp/stocks-export" method="post"
                      enctype="multipart/form-data">
                    <input type="hidden" name="_token" value="<?php echo csrf_token(); ?>">
                    <div class="space-4"></div>
                    <div class="form-group">
                        <label class="col-sm-3 control-label no-padding-right">产品模板 :</label>
                        <div class="col-sm-6">
                        <table id="sample-table-1" class="table table-striped table-bordered table-hover">
                            <thead>
                            <tr>
                                <th></th>
                                <th>品牌名称</th>
                                <th>模板分类</th>
                                <th>模板名称</th>
                                <th>模板编号</th>
                            </tr>
                            </thead>
                            <tbody id="temp_div">
                             @foreach($product_temps as $product_temp)
                                <tr data-tr-id="{{isset($product_temp->product_temp_id)?$product_temp->product_temp_id:''}}">
                                    <td class="center">
                                        <label>
                                            <input type="checkbox"  value="{{isset($product_temp->product_temp_id)?$product_temp->product_temp_id:''}}" class="temp_ids" name="temp_ids[]"/>
                                        </label>
                                    </td>
                                    <td>{{isset($product_temp->brand_name)?$product_temp->brand_name:''}}</td>
                                    <td>{{isset($product_temp->category_name)?$product_temp->category_name:''}}</td>
                                    <td>{{isset($product_temp->temp_name)?$product_temp->temp_name:''}}</td>
                                    <td>{{isset($product_temp->product_temp_id)?$product_temp->product_temp_id:''}}</td>
                                </tr>
                             @endforeach
                            </tbody>
                        </table>
                    </div>
                    </div>
                    <div class="clearfix form-actions">
                        <div class="col-md-offset-5">
                            <button class="btn btn-info btn-export" id="submit" type="button">
                                <i class="icon-ok bigger-110"></i>
                                导出
                            </button>
                        </div>
                    </div>
                </form>
                <!-- PAGE CONTENT ENDS -->
            </div><!-- /.col -->
        </div><!-- /.row -->
    </div><!-- /.page-content -->
@stop
@section('script')
    <script type="text/javascript">
        var _token = '{{csrf_token()}}';
        $(function(){
            var  save_state=1;
            $('.btn-export').click(function() {
                if(save_state==0){
                    swal({title: '请先刷新~~~~', text: "",type: "warning", timer:2000 });
                    return false;
                }
                save_state==0;
                var temp_ids = new Array();
                var i=0;
                $('tbody tr input:checkbox').closest('table').find('tr > td:first-child input:checkbox')
                    .each(function(){
                        var is_checked=$(this).is(":checked");
                        var temp_id=$(this).closest('tr').attr("data-tr-id");
                        if((is_checked==true)&&(temp_id!='')){
                            temp_ids.push(temp_id);
                        }
                    });
                if(temp_ids.length==0){
                    swal({title: '请先选择模板~~~~', text: "",type: "warning", timer:2000 });
                    save_state=1;
                    return;
                }
                $.ajax({
                    type: "POST",
                    cache: true,
                    url: "/backstage/product-temp/stocks-export",
                    data:{'temp_ids':temp_ids},
                    success: function (msg) {
                        if(msg.state==1){
                            swal({title: "保存成功！", text: "",type: "success", timer:1000 });
                            var url=msg.excel_url;
                            downloadFile(url);
                        }else {
                            swal({title: msg.message, text: "",type: "error", timer:1000 });
                        }
                    }, error: function () {
                        swal({title: "系统繁忙！", text: "",type: "error", timer:1000 });
                    }
                });
                save_state==1;
            });
            $('#filter_btn').click(function() {
                var url_para='';
                var brand_id=parseInt($('#brand_id option:selected').val());
                var category_id=parseInt($('#category_id option:selected').val());

                window.location.href="/backstage/product-temp/stocks-export?brand_id="+brand_id+'&category_id='+category_id;
            });
        });
        /**
         * 下载Excel
         * @param url
         */
        function downloadFile(url){
            if(url==null||url==undefined||url==''){
                swal({title:'系统繁忙，请稍后再试！', text: "",type: "error", timer:2000 });
                return;
            }
            var iframe = document.createElement("iframe");
            document.body.appendChild(iframe);
            iframe.src =url;
            setTimeout(function(){
                document.body.removeChild(iframe);
            },3000);
        }
    </script>
@stop